import { applyMiddleware, legacy_createStore } from 'redux';
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import { ALLSTATETYPE, ALLACTIONTYPE, SETLIST, SETFOCUS } from '../types/store.d'

const initialState = {
    list: [],
    focus: []
}

const reducer = (state: ALLSTATETYPE = initialState, { type, payload }: ALLACTIONTYPE) => {
    switch (type) {
        case SETLIST:
            state.list = payload
            return { ...state }
        case SETFOCUS:
            const newfocusList = [...state.focus]
            if (!newfocusList.includes(payload)) {
                newfocusList.push(payload)
                state.focus = newfocusList
            }
            return { ...state }
        default:
            return state
    }
}
const store = legacy_createStore(reducer, applyMiddleware(thunk, logger))
export default store